package com.teddy.DTXMB.Controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.teddy.DTXMB.Model.Custom;
import com.teddy.DTXMB.Model.Order;
import com.teddy.DTXMB.Model.OrderList;
import com.teddy.DTXMB.Service.OrderService;

@Controller
@SessionAttributes("custom")
public class PayMentController {
	/*Log实例*/
	private static final Logger logger = LoggerFactory.getLogger(PayMentController.class);
	
	@Autowired
	private OrderService orderService;
	
	@RequestMapping(value = "/pay/{orderid}", method = RequestMethod.GET)
	public String payOrder(@PathVariable(value="orderid") int orderID, HttpServletRequest request, Model model){
		/*判断用户合法性*/
		Custom custom = (Custom) request.getSession().getAttribute("custom");
		if(custom != null){
			if(custom.getName() != null){
				/*从DB中取出Order以及相关的OrderList*/
				Order order = orderService.getOrderByID(orderID);
				List<OrderList> orderList = orderService.getOrderListByOrderID(orderID);
				logger.info("Payment - Order ID: " + Integer.toString(orderID));
				for(int i=0; i<orderList.size();i++){
					logger.info("Payment - OrderList ID: " + Integer.toString(i) + " - " + orderList.get(i).getName());
				}
				model.addAttribute("order", order);
				model.addAttribute("orderList", orderList);
				return "pay";
			}
			else{
				return "errorcustom";
			}
		}
		else
		{
			return "errorcustom";
		}	
	}
}
